import { login, logout, getInfo } from "@/api/login";
import { setOrg, removeOrg , getToken, setToken, removeToken, getName, setName, setId, removeId, removeName, getAuthority, setAuthority, removeAuthority } from "@/utils/auth";
import defAva from "@/assets/images/profile.jpg";
import { remove } from "nprogress";

const useUserStore = defineStore("user", {
  state: () => ({
    token: getToken(),
    name: getName(),
    avatar: "",
    roles: [],
    id: "",
    permissions: [],
  }),
  actions: {
    // 登录
    login(userInfo) {
      const account = userInfo.username.trim();
      const password = userInfo.password;
      const code = userInfo.code;
      const uuid = userInfo.uuid;
      return new Promise((resolve, reject) => {
        login(account, password, code, uuid)
          .then((res) => {
            this.name = res.login_name;
            this.id = res.id;
            setToken(res.token);
            setOrg(res.curOrg)
            res.resourceVos.forEach((item) => {
              item.meta = {};
              item.meta.title = item.title;
              item.meta.icon = item.icon;
              delete item.title;
              delete item.icon;
              if (item.children) {
                item.children.forEach((val) => {
                  val.meta = {};
                  val.meta.title = val.title;
                  val.meta.icon = val.icon;
                  delete val.title;
                  delete val.icon;
                });
              }
            });
            localStorage.setItem("authority", JSON.stringify(res.resourceVos));
            setName(res.login_name);
            setId(res.id);
            this.token = res.token;
            resolve();
          })
          .catch((error) => {
            reject(error);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
    // 获取用户信息
    getInfo() {
      return new Promise((resolve, reject) => {
        getInfo().then((res) => {

          const user = res.user;
          const avatar = user.avatar == "" || user.avatar == null ? defAva : import.meta.env.VITE_APP_BASE_API + user.avatar;

          if (res.roles && res.roles.length > 0) {
            // 验证返回的roles是否是一个非空数组
            this.roles = res.roles;
            this.permissions = res.permissions;
          } else {
            this.roles = ["ROLE_DEFAULT"];
          }
          this.name = user.userName;
          this.avatar = avatar;
          resolve(res);
        });
      });
    },
    // 退出系统
    logOut() {
      return new Promise((resolve, reject) => {
        logout(this.token)
          .then(() => {
            this.token = "";
            this.id = "";
            this.roles = [];
            this.permissions = [];
            removeToken();
            removeId();
            removeName();
            resolve();
            removeOrg()
            localStorage.removeItem("authority");
          })
          .catch((error) => {
            reject(error);
          })
          .catch((error) => {
            reject(error);
          });
      });
    },
  },
});

export default useUserStore;
